﻿/**
 * Cloud
 *
 * @author		Allen Chou
 * @version		1.0.0 (last update: Jul 5 2008)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

package idv.cjcat.display {
	import flash.display.*;
	import flash.events.*;
	
	public class Cloud extends MovieClip {
		
		public var ampX:Number;
		public var ampY:Number;
		public var freqX:Number;
		public var freqY:Number;
		public var phaseX:Number;
		public var phaseY:Number;
		
		public var time:uint = 0;
		
		private var _x:Number;
		private var _y:Number;
		
		public function Cloud(ampX:Number = 5, ampY:Number = 5, freqX:Number = NaN, freqY:Number = NaN, phaseX:Number = NaN, phaseY:Number = NaN) {
			this.ampX = ampX;
			this.ampY = ampY;
			this.freqX = (isNaN(freqX))?(0.553 + Math.random() * 0.05):(freqX);
			this.freqY = (isNaN(freqY))?(0.443 + Math.random() * 0.05):(freqY);
			this.phaseX = (isNaN(phaseX))?(Math.random() * Math.PI * 2):(phaseX);
			this.phaseY = (isNaN(phaseY))?(Math.random() * Math.PI * 2):(phaseY);
			
			this._x = super.x;
			this._y = super.y;
			
			startFloat();
		}
		
		public function startFloat():void {
			addEventListener(Event.ENTER_FRAME, float);
		}
		
		public function stopFloat():void {
			addEventListener(Event.ENTER_FRAME, float);
		}
		
		public function float(e:Event):void {
			time++;
			super.x = _x + ampX * Math.sin(freqX * time / 20 + phaseX);
			super.y = _y + ampY * Math.sin(freqY * time / 20 + phaseY);
		}
		
		override public function get x():Number { return super.x; }
		override public function set x(value:Number):void {
			_x = value;
			super.x = _x + ampX * Math.sin(freqX * time / 20 + phaseX);
		}
		override public function get y():Number { return super.y; }
		override public function set y(value:Number):void {
			_y = value;
			super.y = _y + ampY * Math.sin(freqY * time / 20 + phaseY);
		}
		
	}
}